package org.itboys.core.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.core.entity.mongo.SundayStore;
import org.itboys.core.entity.mongo.SundayUser;
import org.itboys.core.service.mongo.web.SundayStoreService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：贾琦磊
 * @Date：2018/8/1
 * @Description：仓库管理接口
 */
@Controller
@RequestMapping("/sunday/web/store")
public class SundayStoreController extends BaseController{
    @Autowired
    private SundayStoreService sundayStoreService;

    @RequestMapping("/index" )
    public  String index(HttpServletRequest request){
        return "/sunday/store/index";
    }

    @RequestMapping("/input" )
    public  String input(HttpServletRequest request
            ,@RequestParam("id") Long id
            ,Model model){
        model.addAttribute("store",sundayStoreService.getById(id));
        return "/sunday/store/input";
    }
    /**
     * 新增或修改仓库信息
     * @param request
     * @param response
     */
    @RequestMapping("/save")
    public  void save(HttpServletRequest request,
                      HttpServletResponse response,
                      @RequestParam(value="id") Long id
                        ,@RequestParam(value="name") String name
                        ,@RequestParam(value="orgType") Integer orgType
                        ,@RequestParam(value="user") String user
                        ,@RequestParam(value="mobile") String mobile
                        ,@RequestParam(value="type") Integer type
                        ,@RequestParam(value="provinceId") Long provinceId
                        ,@RequestParam(value="provinceName") String provinceName
                        ,@RequestParam(value="cityId") Long cityId
                        ,@RequestParam(value="cityName") String cityName
                        ,@RequestParam(value="districtId") Long districtId
                        ,@RequestParam(value="districtName") String districtName
                        ,@RequestParam(value="address") String address
                        ,@RequestParam(value="longitude") Double longitude
                        ,@RequestParam(value="latitude") Double latitude
                      ,@RequestParam(value="imageFile",required=false) MultipartFile imageFile
    ) {
        try {
            SundayStore store=new SundayStore( name, orgType, null,
                    user, mobile, type,
                     provinceId,  provinceName,  cityId,
                     cityName,  districtId,
                     districtName,  address,
                     longitude,  latitude);
            sundayStoreService.save(id,store,imageFile);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/store/save------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
        }
    }
    @RequestMapping("/select")
    public void select(HttpServletRequest request
         , HttpServletResponse response)throws Exception{

            PageResult<SundayStore> result = sundayStoreService.selectAll(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);

    }

    /**
     * 仓库选择器，目前业务逻辑只考虑单选
     * @param request
     * @param isSingle
     * @param param
     * @param model
     * @return
     */
    @RequestMapping("/choose" )
    public  String choose(HttpServletRequest request,
                          @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
                          @RequestParam(value="callBack",required=false)String callBack,//回调参数
                          @RequestParam(value="param",required=false)String param,//查询参数
                          Model model){
        model.addAttribute("param",param);
        model.addAttribute("callBack",callBack);
        if(isSingle==null){
            return "/sunday/store/choose/choose_erro";
        }
        return "/sunday/store/choose/choose_single";
    }
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       @RequestParam(value="id",required=true) Long id){
        try {
            sundayStoreService.delete(id);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/store/delete------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
        }
    }
}
